{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a84f2b1c-2995-4643-bd82-a5494477838e",
   "metadata": {},
   "source": [
    "# Python 的数据结构"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad515e7b-bbe8-4b55-a5e7-3b82201d2df2",
   "metadata": {},
   "source": [
    "## 列表(list)\n",
    "1.创建，[]\n",
    "2.提取数据，索引值\n",
    "3.提取-》批数  [初值：终值：步长]\n",
    "4.range -> list一个列表   x= [] or x=list()  \n",
    "5.len() ->length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b97d3711-17fc-4bba-a9e3-46c42cf6e6b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2]\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "A =[0,1,2,3,4]\n",
    "\n",
    "print(A[1:3]) #取左<=()<右,Python特有的左闭右合\n",
    "\n",
    "print(A[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "cb2f840f-e075-4cfa-9eec-0cd3b81e1015",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]\n"
     ]
    }
   ],
   "source": [
    "z =list(range(0,101))\n",
    "print (z[1:101:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "6bc46d8b-22ea-45a0-90e2-0decad9f530a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "a =[1,2,3]\n",
    "b =[3,4]\n",
    "c = a * 2\n",
    "print (c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "2eacf6b8-f208-4fcd-a48b-b67b6b5ca6e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n",
      "[1, 5]\n",
      "[1, 2, 3, 4, 5]\n",
      "[2, 3, 4, 5]\n",
      "[2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "a =[]\n",
    "a.append(1)#a.append单次只添加一个元素，包括另一个list\n",
    "print (a)\n",
    "a.append(5)\n",
    "print (a)\n",
    "a.insert(1,2)#a.insert（i，j） 在第i+1位插入一个j\n",
    "a.insert(2,3)\n",
    "a.insert(3,4)\n",
    "print (a)\n",
    "a.pop (0) #脱离第0项\n",
    "print (a)\n",
    "a.remove(5)\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "af1a7299-fb9b-4ed7-97a6-3ef2b41dddc5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "bd830130-3c8f-4810-9352-7c5fcfe8632e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3]\n"
     ]
    }
   ],
   "source": [
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "3c1d8376-050e-43e0-ae34-df1cb2bb3825",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "a =[1,3,5,7,9,2,4,6,8,10]\n",
    "a.sort(reverse=True)\n",
    "print (a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "b0561e5e-8b9e-499e-8f30-03a57f06b243",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "x =0\n",
    "for i in range(101):\n",
    "    x +=i\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "e65c6826-8ed4-41bf-9a80-713f3e6de05f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "x =list(range(1,101))\n",
    "\n",
    "print (sum(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8d5070d-0d88-4578-9a2e-36a955908d9a",
   "metadata": {},
   "source": [
    "## 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4ec94e14-0193-4002-bd2a-5436f03ec0a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5)\n",
      "[1, 2, 3, 0, 5]\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "a = (1,2,3,4,5)\n",
    "b =[1,2,3,4,5]\n",
    "print (a)\n",
    "b[3]=0\n",
    "print (b)\n",
    "print (b[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f6f6f8e-50d4-4882-a9d6-36ab6d6727f9",
   "metadata": {},
   "source": [
    "## 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "385bbd47-6e80-4732-9a7a-87ccd0395785",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "荔枝\n"
     ]
    }
   ],
   "source": [
    "fruits ={\n",
    "    'apple' :'苹果',\n",
    "    'lichi' :'荔枝',\n",
    "}\n",
    "print (fruits['lichi'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c2ea273-7690-4bfc-86c7-4f33dcc094ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入一种颜色的英文： pink\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对应的意思是: 粉色\n"
     ]
    }
   ],
   "source": [
    "colors ={\n",
    "    'white':'白色',\n",
    "    'black':'黑色',\n",
    "    'gray':'灰色',\n",
    "    'yellow':'黄色',\n",
    "    'blue':'蓝色',\n",
    "    'red':'红色',\n",
    "    'green':'绿色',\n",
    "    'orange':'橙色',\n",
    "    'pink':'粉色',\n",
    "}\n",
    "\n",
    "i=input(\"输入一种颜色的英文：\")\n",
    "\n",
    "print (f\"对应的意思是:\",colors[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff21bf94-e644-46ea-9dd8-7776ac32dddf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 作业\n",
    "1.实现一个完整的词典\n",
    "2.输入英文，输出中文\n",
    "3.实现一直输入，直到一个可行\n",
    "4.考虑容错\n",
    "5.友好的中文提示"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19841d31-e86c-49cb-8de2-fdc30570d2b6",
   "metadata": {},
   "source": [
    "# 第五次作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a714699-e3b5-48fc-8cc3-55755f08c8a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请以全小写字母的形式输入你需要查询的一种颜色的单词，输入'.'则退出\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " red\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的单词是red对应的中文是，红色\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " green\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的单词是green对应的中文是，绿色\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " GREEN\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的单词是green对应的中文是，绿色\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " Gree n\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "可能输入了非字母成分，请检查一次\n",
      "未能找到这个单词，请检查是否拼写有误\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "  green\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的单词是green对应的中文是，绿色\n"
     ]
    }
   ],
   "source": [
    "colors={\n",
    "    'white':'白色',\n",
    "    'black':'黑色',\n",
    "    'gray':'灰色',\n",
    "    'yellow':'黄色',\n",
    "    'blue':'蓝色',\n",
    "    'red':'红色',\n",
    "    'green':'绿色',\n",
    "    'orange':'橙色',\n",
    "    'pink':'粉色',\n",
    "    \n",
    "}\n",
    "print(\"请以全小写字母的形式输入你需要查询的一种颜色的单词，输入'.'则退出\")\n",
    "while True:\n",
    "    i =input()\n",
    "    i =i.strip()\n",
    "    i =i.lower()#初步排除不规范\n",
    "    if i ==\".\":\n",
    "        break\n",
    "    if i.isalpha()== False:\n",
    "        print(\"可能输入了非字母成分，请检查一次\")\n",
    "    if i in colors:\n",
    "        print(f\"你输入的单词是{i}对应的中文是，{colors[i]}\")\n",
    "    else:\n",
    "        print(\"未能找到这个单词，请检查是否拼写有误\")\n",
    "        \n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22b794a8-f854-4334-993b-f9e09ea8e07f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
